home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
prtprev
/
genprint.txt
< prev
next >
Wrap
Text File
|
1994-04-03
|
19KB
|
539 lines
How to Add Print Preview to Visual Basic Applications [B_VBasic]
ID: Q113236 CREATED: 29-MAR-1994 MODIFIED:
2.00 3.00
Windows
ENDUSER |
--------------------------------------------------------------
The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic
Programming System for Windows, version 2.0 and 3.0
--------------------------------------------------------------
SUMMARY
=======
This article describes how to create printing routines that can
print to the printer or to a picture box. This enables you to add
print preview capabilities to your Visual Basic applications.
There are several ways that you could implement print preview in
your applications. This article describes one method that is easy
to do in Visual Basic and works well.
MORE INFORMATION
================
Generic Printing
----------------
It would be ideal to have a generic print routine that could print
to the printer or to the screen depending on what you pass it. The
Visual Basic printer object and picture box control have many of the
same methods and properties. For example, both of these are valid:
Printer.Print AString
Picture1.Print AString
It would be nice if you could pass a generic object to a subroutine
and the subroutine would use the Print method off of the generic object
as in this example:
Call PrintJob(Printer)
Call PrintJob(Picture1)
Sub PrintJob(GenericObject As Object)
GenericObject.Print AString
End Sub
Unfortunately, this is not possible. The Visual Basic Printer object
is a system object, so it can't be passed as a parameter.
This leaves you with two choices in Visual Basic. You could create two
routines -- one for printing to the printer and one for print preview.
However, the code would not be reusable in your future projects. The
second approach is to write your own set of routines that can print to
the printer or a picture box based on the value of a flag. This is the
method used in the example code given below. Once you create the
routines, you can re-use them in future programs.
The example creates routines that closely mimic Visual Basic's built in
methods and properties. However, you could use this approach to create
high-level routines that greatly simplify your printing needs.
The routines work by checking the variable PrinterFlag. PrinterFlag is
True when printing is going to the printer and False when printing to
the picture box.
Here's the print routine from the example. Notice how it is just a
shell function that determines what to print to and then does it.
Sub PrintPrint (PrintVar)
If PrinterFlag Then
Printer.Print PrintVar
Else
objPrint.Print PrintVar
End If
End Sub
With just a few simple routines like this, you can start to do
generic printing.
Scaling
-------
To accomplish print preview, the program must scale the output to the
picture box to match the output on the printer.
In the example, the PrintStartDoc routine initializes the printer or
picture box and sets up the scaling. The width and height of the paper
are passed to the PrintStartDoc routine. These dimensions are used to
determine the non-printable area of the printer object, find the ratio
of the picture box to the printer, re-size the picture box, and scale
the picture box. The picture box is scaled with the Scale method. After
setting the scale of the picture box, graphic methods use the new
coordinates. For an 8.5 x 11 inch piece of paper the picture box is
scaled with this command:
Picture1.Scale (0, 0)-(8.5, 11)
The Scale method does not scale fonts. To scale the fonts, use the
ratio of the picture box height divided by the printer's height in
inches. Then multiply by this ratio to determine the correct font
size within the picture box. Here is the PrintFontSize routine that
sets the appropriate font sizes in the example:
Sub PrintFontSize (pSize)
If PrinterFlag Then
Printer.FontSize = pSize
Else
'Sized by ratio since Scale method does not effect FontSize
ObjPrint.FontSize = pSize * Ratio
End If
End Sub
The ratio used to calculate the font size can be applied to anything
you need to scale in the picture box that is not automatically scaled
by the Scale method. The ratio is also used in the PrintPicture routine
to scale pictures.
Step-by-Step Example
--------------------
1. Start a new project in Visual Basic. Form1 is created by default.
2. Add a command button (Command1), a check box (Check1), and two
picture boxes (Picture1 and Picture2) to the form.
3. Put the following code in the command button click event:
Sub Command1_Click ()
'Setup (Could be done at design time or in form load)
'Make printing stick
Picture1.AutoRedraw = True
'Add a palette for 256 colors
Picture1.Picture = LoadPicture("C:\VB\PASTEL.DIB")
'Setup hidden picture
Picture2.AutoRedraw = False
Picture2.ScaleMode = 3 'Pixels
Picture2.Visible = False
Picture2.AutoSize = True
Picture2.Picture = LoadPicture("C:\VB\METAFILE\BUSINESS\PRINTER.WMF")
'This print job can go to the printer or the picture box
If Check1.Value = 0 Then PrinterFlag = True
PrintStartDoc Picture1, PrinterFlag, 8.5, 11
'All the subs use inches
PrintBox 1, 1, 6.5, 9
PrintLine 1.1, 2, 7.4, 2
PrintPicture Picture2, 1.1, 1.1, .8, .8
PrintFilledBox 2.1, 1.2, 5.2, .7, RGB(200, 200, 200)
PrintFontName "Arial"
PrintCurrentX 2.3
PrintCurrentY 1.3
PrintFontSize 35
PrintPrint "Visual Basic Printing"
For x = 3 To 5.5 Step .2
PrintCircle x, 3.5, .75
Next
PrintFontName "Courier New"
PrintFontSize 30
PrintCurrentX 1.5
PrintCurrentY 5
PrintPrint "It is possible to do"
PrintFontSize 24
PrintCurrentX 1.5
PrintCurrentY 6.5
PrintPrint "It is possible to do print"
PrintFontSize 18
PrintCurrentX 1.5
PrintCurrentY 8
PrintPrint "It is possible to do print preview"
PrintFontSize 12
PrintCurrentX 1.5
PrintCurrentY 9.5
PrintPrint "It is possible to do print preview with good results."
PrintEndDoc
End Sub
4. Add a new Module to the project (MODULE1.BAS).
5. Put the following code in the basic module:
Option Explicit
' The following Types, Declares, and Constants are only necessary for the
' PrintPicture routine
'=========================================================================
=
Type BITMAPINFOHEADER_TYPE
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
bmiColors As String * 1024
End Type
Type BITMAPINFO_TYPE
BitmapInfoHeader As BITMAPINFOHEADER_TYPE
bmiColors As String * 1024
End Type
'Each of the following declares should be entered on a single line
Declare Function GetDIBits Lib "gdi" (ByVal hDC As Integer,
ByVal hBitmap As Integer, ByVal nStartScan As Integer,
ByVal nNumScans As Integer, ByVal lpBits As Long,
BitmapInfo As BITMAPINFO_TYPE, ByVal wUsage As Integer) As Integer
Declare Function StretchDIBits Lib "gdi" (ByVal hDC As Integer,
ByVal DestX As Integer, ByVal DestY As Integer,
ByVal wDestWidth As Integer, ByVal wDestHeight As Integer,
ByVal SrcX As Integer, ByVal SrcY As Integer,
ByVal wSrcWidth As Integer, ByVal wSrcHeight As Integer,
ByVal lpBits As Long, BitsInfo As BITMAPINFO_TYPE,
ByVal wUsage As Integer, ByVal dwRop As Long) As Integer
Declare Function GlobalAlloc Lib "kernel" (ByVal wFlags As Integer,
ByVal lMem As Long) As Integer
Declare Function GlobalLock Lib "kernel" (ByVal hMem As Integer) As Long
Declare Function GlobalUnlock Lib "kernel"
(ByVal hMem As Integer) As I